﻿Imports System.Data
Imports System.Data.OleDb


Public Class frmEditProjekt

    Dim SQLStr As String
    Private ConnString As String
    Dim _project As Projekt = Projekt.GetObject()

    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=|DataDirectory|\db.accdb")
    Dim reader As OleDbDataReader
    Dim cmd As New OleDbCommand

    Private Sub frmEditProjekt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "DbShowKunden.ShowKunden". Sie können sie bei Bedarf verschieben oder entfernen.
        Me.ShowKundenTableAdapter.Fill(Me.DbShowKunden.ShowKunden)
        'TODO: Diese Codezeile lädt Daten in die Tabelle "DbShowKunden.ShowKunden". Sie können sie bei Bedarf verschieben oder entfernen.
        Me.ShowKundenTableAdapter.Fill(Me.DbShowKunden.ShowKunden)
        If (_project.projectEdit) Then
            SQLStr = "SELECT * FROM [Projekte] WHERE [PK_Projekte] = " + _project.projectrowId
            cmd.CommandText = SQLStr
            cmd.Connection = conn
            cmd.Connection.Open()
            reader = cmd.ExecuteReader()

            Do While reader.Read()
                txbTitel.Text = reader("Titel").ToString()
                lblChoosedKunde.Text = reader("FK_Kunden").ToString()

            Loop
            cmd.Connection.Close()
        End If
    End Sub

    Private Sub frmEditProjekt_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        _project.projectEdit = False
        frmShowProjekte.Show()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If (_project.projectEdit = True) Then
            SQLStr = "UPDATE [Projekte] SET [Titel] = '" + txbTitel.Text + "', [FK_Kunden] = " + cbxKunde.SelectedValue.ToString() + " Where [PK_Projekte] = " + _project.projectrowId
        Else
            SQLStr = "INSERT into [Projekte]([Titel], [FK_Kunden]) VALUES('" + txbTitel.Text + "', " + cbxKunde.SelectedValue.ToString() + ")"
        End If

        'SQLStr = "SELECT * FROM tblQuestion"


        'SQLStr = "UPDATE tblQuestion SET Answer = 'Like this' Where Question = 'How to use SQL?'"

        'SQLStr = "DELETE FROM tblQuestion WHERE Question='How to use SQL?'"
        executeQuery(SQLStr)

    End Sub

    Private Sub executeQuery(ByVal sqlquery As String)
        Try
            cmd.CommandText = SQLStr
            cmd.Connection = conn
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

End Class